package net.huanger.wizard;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;

public class Tools
{
  public static String getCurrentFolder(ISelection selection)
  {
        if (selection instanceof StructuredSelection)
        {
          Object firstElement = ((StructuredSelection)selection).getFirstElement();
          IResource resource = null;
          if (firstElement instanceof IAdaptable)
          {
            IAdaptable adaptable = (IAdaptable)firstElement;
            resource = (IResource)adaptable.getAdapter(IResource.class);
          }
          else if (firstElement instanceof IResource)
          {
            resource = (IResource)firstElement;
          }
          if (resource != null)
          {
            if (resource instanceof IFolder)
            {
              return resource.getFullPath().toString();
            }
            else if (resource instanceof IFile)
            {
              String string = ((IFile)resource).getParent().getFullPath().toString();
              return string;
            }
          }
        }
        return null;
      
  }
}
